home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4242 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.1 KB  |  145 lines

  1. Path: mother.usf.edu!news
  2. From: Chris Frenck <cfrenck@soleil.acomp.usf.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Command line arg and segmentation fault
  5. Date: Fri, 02 Feb 1996 15:27:36 -0500
  6. Organization: University of South Florida
  7. Message-ID: <311273B8.25BF@soleil.acomp.usf.edu>
  8. NNTP-Posting-Host: ppp43.cfr.usf.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13. CC: cfrenck@soleil.acomp.usf.edu
  14.  
  15. I am a beginning C student and have run into a bit of a problem. When compiling on GCC 
  16. (required) I get a segmentation fault. If I compile with MSVC++ at home it runs fine. 
  17. Could someone please tell me what the error is?
  18.  
  19. TIA, Chris cfrenck@soleil.acomp.usf.edu
  20.  
  21.  Here is my code:
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #define MAX 30 /* Maximum reversible argument length */         
  27.  
  28.  
  29.     /* Function Prototype */
  30. void check(int argc,char *argv[]); /*will move later */
  31.  
  32.  
  33. main(int argc, char *argv[])
  34. {
  35.        check(argc,argv);
  36.     return(0);
  37. }
  38.  
  39. /***Start check function ****************************************/
  40. /* This function will ensure that command line arguments have   */
  41. /* been input and directed                                      */ 
  42. /****************************************************************/    
  43. void check(int argc,char *argv[])
  44. {                           
  45.     /* Variable declarations */
  46.     char fwd[]="forward";
  47.     char bkwd[]="backward";    
  48.     int count;    /* How many command line arguments */
  49.     
  50.     /*Function Prototype */
  51.     void forward(int count,char *argv[]);
  52.     void reverse(int count,char *argv[]);
  53.     
  54.     if (argc > 1)
  55.         { for (count = 1; count < argc; count++); /* counts arguments */
  56.         }
  57.         
  58.         else    
  59.             printf("No command line arguments to process.");
  60.     
  61.     if ((strcmp(argv[1],fwd)!=0) && (strcmp(argv[1],bkwd)!=0))
  62.         { printf("\nYou must enter either forward or backward ");
  63.           printf("as your first argument after the file name.\n");
  64.           printf("Please Try it again.");
  65.         }    
  66.     
  67.     if (strcmp(argv[1],fwd)==0) /* if 2nd argument is forward */
  68.         forward(count,argv);       
  69.        
  70.        
  71.     if (strcmp(argv[1],bkwd)==0)/* if 2nd argument is backward */
  72.         reverse(count,argv);
  73.        
  74.     return;
  75. }
  76. /***End check funtion *******************************************/
  77.  
  78. /***Start forward function **************************************/
  79. /* This function will print the command line arguments forwards */
  80. /****************************************************************/
  81. void forward(int count,char *argv[])
  82. {   
  83.     
  84.     int tempctr;
  85.     
  86.          for (tempctr=2; tempctr < count; tempctr++)
  87.              { printf("%s ", argv[tempctr]);
  88.              }
  89.                   
  90.     return;
  91.           
  92. }
  93. /***End forward function ****************************************/
  94.  
  95. /***Start reverse function **************************************/
  96. /* This funtion will print the command line arguments in reverse*/
  97. /* For example: one two => two one                              */
  98. /****************************************************************/
  99. void reverse(int count,char *argv[])
  100. {   
  101.     /* Variable Declarations */
  102.     char *argument;
  103.     int tempctr;
  104.     char *revphrase; 
  105.     
  106.     /* Function Prototype */
  107.     char palin(char [], char []);    
  108.     
  109.       for (tempctr=count; count > 1; count--)
  110.           { argument= argv[count];
  111.                palin(argument,revphrase); 
  112.               printf("%s ", revphrase);
  113.             tempctr++;
  114.           } 
  115.           
  116.     return;
  117.      
  118. }
  119. /***End reverse function ****************************************/    
  120.  
  121.  
  122. /***Start palin function ****************************************/
  123. /* This function will actually reverse lettering i.e.,the=>eht  */
  124. /****************************************************************/
  125. char palin(char *argument, char *revphrase)
  126. {
  127.    /* Variable declarations */
  128.     int ctr1=0;
  129.     int ctr2=0;    
  130.                                 /*SEGMENTATION FAULT IN FOR LOOP    */
  131.                 /* I also get a seg. fault if just  */
  132.                 /* the program name is entered and  */
  133.                 /* nothing else                */
  134.  
  135.     for (ctr1 = strlen (argument)-1; ctr1 >=0; ctr1--) /*reverses*/
  136.         { revphrase[ctr1]= argument[ctr2];
  137.           ctr2++;
  138.         }
  139.     revphrase[ctr2]='\0';
  140.                 
  141.     return(*revphrase);
  142.  
  143. }
  144. /*** End palin function *****************************************/
  145.